'use server';
import { PrismaClient, Student } from '@/data/prisma';
import { formatFindParams } from '@/pao/server/serverPrisma';

const prisma = new PrismaClient()

export async function query (filter: Record<string, any>
    , sorter: Record<string, any>
    , pageSize: number = 50
    , pageIndex: number = 1
    , columnFilterTypes: Record<string, string> = {}): Promise<{ rows: Student[]; total: number; }>  {
    const students = await prisma.student.findMany(formatFindParams(filter, sorter, pageSize, pageIndex, columnFilterTypes))
    return {rows:students as unknown as Student[], total: students.length}
}

export async function deleteRow (row: Student) {
    await prisma.student.delete({ where: { id: row.id } })
}

export async function editRow (row: Student) {
    await prisma.student.update({  where: { id: row.id }, data: row })
}

export async function addRow(row: Student) {
    await prisma.student.create({ data: row })
}